package testplan.servlets;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.json.JSONException;
import org.json.JSONObject;
import testplan.entities.TestCase;
import testplan.entities.User;
import testplan.services.ProjectService;
import testplan.services.TestPlanService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Singleton
public class TestPlanServlet extends HttpServlet
{
	@Inject
	private TestPlanService service;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
		checkNotNull(service);
        
        final String action = request.getParameter("action");
        try
        {
	        if("create".equals(action))
	        {
	        	doCreate(request, response);
	        }
	        else if("list".equals(action))
	        {
	        	doList(request, response);
	        }
            else if ("addCase".equals(action))
            {
                doAddCase(request, response);
            }
            else if ("getCases".equals(action))
            {
                doGetCases(request, response);
            }
        }
        catch(JSONException e)
        {
        	throw new RuntimeException(e);
        }
    }

    private void doGetCases(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException
    {
        final String planId = request.getParameter("plan");
        checkNotNull(planId);

        List<Map> testCaseList = new ArrayList<Map>();
        for(TestCase testCase : service.getTestCases(Long.valueOf(planId)))
        {
            Map<String, Object> testCaseInfo = new HashMap<String, Object>();
            testCaseInfo.put("title", testCase.getTitle());
            testCaseInfo.put("description", testCase.getDescription());
            testCaseInfo.put("steps", testCase.getSteps());
            testCaseInfo.put("expectedResults", testCase.getExpectedResults());
            testCaseList.add(testCaseInfo);
        }

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("planId", planId);
        result.put("testCases", testCaseList);
        response.getOutputStream().println(new JSONObject(result).toString(2));
    }

    private void doAddCase(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        final String title = request.getParameter("title");
        final String description = request.getParameter("desc");
        final String steps = request.getParameter("steps");
        final String expectedResults = request.getParameter("expect");
        final User user = (User) request.getSession().getAttribute("user");
        final String planId = request.getParameter("plan");

        checkNotNull(title);
        checkNotNull(description);
        checkNotNull(steps);
        checkNotNull(expectedResults);
        checkNotNull(user);
        checkNotNull(planId);

        service.addTestCase(title, description, steps, expectedResults, user.getKey(), Long.valueOf(planId));
        response.getOutputStream().println("OK");
    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException
    {

    }

    private void doCreate(HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException
    {
        final String name = request.getParameter("name");
        final String description = request.getParameter("desc");
        final String projectId = request.getParameter("project");
        final String releaseId = request.getParameter("release");
        final User user = (User) request.getSession().getAttribute("user");

        checkNotNull(name);
        checkNotNull(description);
        checkNotNull(projectId);
        checkNotNull(releaseId);
        checkNotNull(user);

        service.createTestPlan(name, description, Long.valueOf(projectId), Long.valueOf(releaseId), user.getKey());
        response.getOutputStream().println("OK");        
    }
}